package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.realspace.Camera;
import com.supermap.realspace.Scene;
import com.supermap.realspace.spatialanalyst.AnalysisQuality;
import com.supermap.realspace.spatialanalyst.Skyline;
import com.supermap.realspace.spatialanalyst.SkylineDisplayMode;
import com.supermap.ui.SceneControl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SkylineAnalystDialog extends CJDialog {
    private SceneControl sceneControl;
    private JComboBox<AnalysisQuality> qualityCmb;
    private JComboBox<SkylineDisplayMode> displayModeCmb;
    private JCheckBox view360Cbx;

    private Color skylineColor;
    private Skyline skyline;

    public SkylineAnalystDialog(Frame owner, String title, boolean modal, SceneControl sceneControl) {
        super(owner, title, modal, 300, 260, new Object[]{sceneControl});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.sceneControl = (SceneControl) args[0];
        this.skylineColor = Color.BLUE;
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                if (skyline != null) {
                    skyline.clear();
                    skyline.dispose();
                }
            }
        });
        super.confirmBtn.setText("分析");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        add(mainPanel, BorderLayout.CENTER);

        JLabel viewerPointLbl = new JLabel("观察者位置: ");
        mainPanel.add(viewerPointLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JTextField viewerPointTxt = new JTextField();
        viewerPointTxt.setText("相机位置");
        viewerPointTxt.setEnabled(false);
        mainPanel.add(viewerPointTxt, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel skylineColorLbl = new JLabel("天际线颜色: ");
        mainPanel.add(skylineColorLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JButton skylineBtn = new JButton();
        skylineBtn.setBackground(skylineColor);
        skylineBtn.addActionListener(e -> {
            skylineColor = JColorChooser.showDialog(this, "天际线颜色", Color.BLUE);
            if (skylineColor != null) {
                skylineBtn.setBackground(skylineColor);
            }
        });
        mainPanel.add(skylineBtn, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel qualityLbl = new JLabel("天际线质量: ");
        mainPanel.add(qualityLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        qualityCmb = new JComboBox<>();
        Enum[] enums = AnalysisQuality.getEnums(AnalysisQuality.class);
        for (Enum anEnum : enums) {
            qualityCmb.addItem((AnalysisQuality) anEnum);
        }
        mainPanel.add(qualityCmb, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel displayModeLbl = new JLabel("显示类型: ");
        mainPanel.add(displayModeLbl, new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        displayModeCmb = new JComboBox<>();
        enums = SkylineDisplayMode.getEnums(SkylineDisplayMode.class);
        for (Enum anEnum : enums) {
            displayModeCmb.addItem((SkylineDisplayMode) anEnum);
        }
        mainPanel.add(displayModeCmb, new GridBagConstraints(1, 3, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        view360Cbx = new JCheckBox("360°显示");
        mainPanel.add(view360Cbx, new GridBagConstraints(0, 4, 2, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));
    }

    @Override
    protected void doConfirmCore() {
        if (sceneControl == null) {
            return;
        }

        Scene scene = sceneControl.getScene();
        Camera camera = scene.getCamera();

        skyline = new Skyline(scene);
        skyline.setColor(skylineColor);
        skyline.setDisplayStyle((SkylineDisplayMode) displayModeCmb.getSelectedItem());
        skyline.setQuality((AnalysisQuality) qualityCmb.getSelectedItem());
        skyline.setLookAround(view360Cbx.isSelected());
        skyline.setViewerVisible(true);
        skyline.setDirection(camera.getHeading());
        Point3D viewerPosition = new Point3D(camera.getLatitude(), camera.getLongitude(), camera.getAltitude());
        skyline.setViewerPosition(viewerPosition);
        if (!skyline.build()) {
            JOptionPane.showMessageDialog(this, "天际线分析失败！", "天际线分析", JOptionPane.WARNING_MESSAGE);
        }
    }
}
